package main

import (
	"encoding/hex"
	"flag"
	"fmt"

	"gitee.com/thubcc/p2pgit/classedpack"
	"gitee.com/thubcc/p2pgit/some"

	"gitee.com/thubcc/p2pgit/gitfs"

	"gitee.com/thubcc/p2pgit/types"
	"github.com/hanwen/go-fuse/v2/fs"
)

var (
	argNewPack = flag.String("n", "../temp", "New Pack Dir")
	argURL     = flag.String("u", "http://localhost:8080", "server url")
	argHash    = flag.String("h", "26a54b59539b31b6e5e897c30b7664e2e9db9cbb", "object hash")
	argMount   = flag.String("m", "../mnt", "mount point")
	argMod     = flag.String("mod", "local", "local or remote")
)

func main() {
	var err error
	flag.Parse()

	var h []byte
	h, err = hex.DecodeString(*argHash)
	if err != nil {
		fmt.Println("Bad Hash", *argHash, err)
		return
	}
	var hash types.Hash
	copy(hash[:], h)

	var gs types.SomeGeter
	switch *argMod {
	case "local":
		gs, err = some.GeterFromDir(*argNewPack)
	case "remote":
		var hs types.SomeHiter
		hs, err = some.HiterFromServer(*argURL)
		if err != nil {
			fmt.Println(err)
		}
		var cs *classedpack.Client
		cs, err = classedpack.NewAPI(*argURL, hs)
		gs = append(gs, cs)
	}
	if err != nil {
		fmt.Println(err)
		return
	}
	rs := types.NewRawer(some.Geter2Rawer(gs))
	rs.Open(false)
	defer rs.Close()

	gitRoot, err := gitfs.NewHashTree(hash, gs)
	if err != nil {
		fmt.Println(err)
		return
	}
	opts := &fs.Options{}
	opts.Debug = true
	server, err := fs.Mount(*argMount, gitRoot, opts)
	if err != nil {
		fmt.Printf("Mount fail: %v\n", err)
	}
	server.Wait()
}
